//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.gui.fixed;

import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import org.jpgolay.me.util.MEDate;

// Referenced classes of package org.jpgolay.gui.fixed:
//            LabelAndComboBox, LabelAndDateComboBox, TwoButton
public class CalendarPeriod extends JInternalFrame {

    public CalendarPeriod() {
        jContentPane = null;
        period = null;
        from = null;
        to = null;
        twoButton = null;
        fromDate = "";
        toDate = "";
        message = "";
        initialize();
    }

    private void initialize() {
        setFrameIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/sch.png")));
        setTitle("S\351l\351ctionner une p\351riode");
        setSize(320, 138);
        setContentPane(getJContentPane());
    }

    private JPanel getJContentPane() {
        if (jContentPane == null) {
            GridLayout gridLayout1 = new GridLayout();
            jContentPane = new JPanel();
            jContentPane.setLayout(gridLayout1);
            gridLayout1.setRows(4);
            gridLayout1.setColumns(1);
            jContentPane.add(getPeriod(), null);
            jContentPane.add(getFrom(), null);
            jContentPane.add(getTo(), null);
            jContentPane.add(getTwoButton(), null);
        }
        return jContentPane;
    }

    private LabelAndComboBox getPeriod() {
        if (period == null) {
            period = new LabelAndComboBox();
            period.setLabel("P\351riode");
            period.setList(new String[]{
                        "TOUT", "ANNEE EN COURS", "MOIS EN COURS", "SEMAINE EN COURS", "DU - AU"
                    });
            period.addPropertyChangeListener("selectedIndex", new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent e) {
                    setPeriod();
                }
            });
        }
        return period;
    }

    private LabelAndDateComboBox getFrom() {
        if (from == null) {
            from = new LabelAndDateComboBox();
            from.setLabel("Du");
            from.setEnabledDateComboBox(false);
        }
        return from;
    }

    private LabelAndDateComboBox getTo() {
        if (to == null) {
            to = new LabelAndDateComboBox();
            to.setLabel("Au");
            to.setEnabledDateComboBox(false);
        }
        return to;
    }

    private TwoButton getTwoButton() {
        if (twoButton == null) {
            twoButton = new TwoButton();
            twoButton.setButtonLabelLeft("VALIDER");
            twoButton.setButtonLabelRight("ANNULER");
        }
        return twoButton;
    }

    private void setPeriod() {
        switch (period.getSelectedIndex()) {
            case 0: // '\0'
                getFrom().setEnabledDateComboBox(false);
                getTo().setEnabledDateComboBox(false);
                getFrom().setDate("");
                getTo().setDate("");
                fromDate = "01/01/1990";
                toDate = "01/01/2090";
                message = "P\351riode : TOUT";
                break;

            case 1: // '\001'
                getFrom().setEnabledDateComboBox(false);
                getTo().setEnabledDateComboBox(false);
                getFrom().setDate("");
                getTo().setDate("");
                toDate = "31/12" + (new MEDate()).getPlain().substring(5);
                fromDate = "01/01" + toDate.substring(5);
                message = "P\351riode : ANNEE EN COURS";
                break;

            case 2: // '\002'
                getFrom().setEnabledDateComboBox(false);
                getTo().setEnabledDateComboBox(false);
                getFrom().setDate("");
                getTo().setDate("");
                toDate = (new MEDate()).getPlain();
                fromDate = "01" + toDate.substring(2);
                message = "P\351riode : MOIS EN COURS";
                break;

            case 3: // '\003'
                getFrom().setEnabledDateComboBox(false);
                getTo().setEnabledDateComboBox(false);
                getFrom().setDate("");
                getTo().setDate("");
                fromDate = (new MEDate()).firstDateOfTheWeek();
                toDate = (new MEDate()).lastDateOfTheWeek();
                message = "P\351riode : SEMAINE EN COURS";
                break;

            case 4: // '\004'
                getFrom().setEnabledDateComboBox(true);
                getTo().setEnabledDateComboBox(true);
                fromDate = "";
                toDate = "";
                break;
        }
    }

    public String getFromDate() {
        if (getFrom().isEnabledDateComboBox()) {
            message = "P\351riode : DU  " + getFrom().getDate() + "  AU  " + getTo().getDate();
            return getFrom().getDate();
        } else {
            return fromDate;
        }
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public String getToDate() {
        if (getTo().isEnabledDateComboBox()) {
            return getTo().getDate();
        } else {
            return toDate;
        }
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

    public JButton getButtonLeft() {
        return getTwoButton().getButtonLeft();
    }

    public JButton getButtonRight() {
        return getTwoButton().getButtonRight();
    }

    public String getMessage() {
        return message;
    }

    public void setSelectedPeriod(int index) {
        getPeriod().setSelectedIndex(index);
        setPeriod();
    }
    private JPanel jContentPane;
    private LabelAndComboBox period;
    private LabelAndDateComboBox from;
    private LabelAndDateComboBox to;
    private TwoButton twoButton;
    private String fromDate;
    private String toDate;
    private String message;
    public static final int ALL = 0;
    public static final int THIS_YEAR = 1;
    public static final int THIS_MONTH = 2;
    public static final int THIS_WEEK = 3;
    public static final int FROM_TO = 4;
}
